/**
 * 添加日期内容清除按钮
 */
var dateButtons = $.extend([], $.fn.datebox.defaults.buttons);
dateButtons.splice(1, 0, {
    text: '清空',
    handler: function (target) {
        $(target).datebox('setValue', '').datebox('hidePanel');
    }
});




/**
 * 预览首页
 * @returns
 */
function preViewIndex(){
	
}

/**
 * 生成首页
 * @returns
 */
function makeIndex(){
	$('#makeIndexForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeIndexLog").text("正在生成首页...");
			}
			return $(this).form('validate');
		},
		success : function(data) {
			$("#makeIndexLog").text(data);
		}
	});
	
}

/**
 * 生成xml地圖
 * @returns
 */
function makeXml(){
	$('#makeXmlForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeXmlLog").text("正在生成地圖...");
			}
			return $(this).form('validate');
		},
		success : function(data) {
			$("#makeXmlLog").text(data);
		}
	});
	
}


/**
 * 生成自定义页面
 * @returns
 */
function makeCustom(){
	$('#makeCustomForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeCustomLog").text("正在生成自定义页面...");
			}
			return $(this).form('validate');
		},
		success : function(data) {
			$("#makeCustomLog").text(data);
		}
	});
	
}


/**
 * 生成栏目页
 * @returns
 */
function makeType(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeTypeForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeTypeLog").text("正在生成栏目页...");
				taskTimer = setInterval(function(){getMakeProcess('type',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeTypeLog").text();
			//$("#makeTypeLog").text(data + currentLog);
			$("#makeTypeLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
	
}

/**
 * 生成汇总栏目
 * @returns
 */
function makeCommonType(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeTypeForm').form('submit', {
		url:'makehtml/commonType',
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeTypeLog").text("正在生成栏目页...");
				taskTimer = setInterval(function(){getMakeProcess('type',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeTypeLog").text();
			//$("#makeTypeLog").text(data + currentLog);
			$("#makeTypeLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
}


function makeCommonArticle(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeArticleForm').form('submit', {
		url:'makehtml/commonArticle',
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeArticleLog").text("正在生成文章页...");
				taskTimer = setInterval(function(){getMakeProcess('article',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeArticleLog").text();
			//$("#makeArticleLog").text(data + currentLog);
			//$("#makeArticleLog").text(data);
			$("#makeArticleLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
}




/**
 * 生成文章页
 * @returns
 */
function makeArticle(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeArticleForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeArticleLog").text("正在生成文章页...");
				taskTimer = setInterval(function(){getMakeProcess('article',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeArticleLog").text();
			//$("#makeArticleLog").text(data + currentLog);
			//$("#makeArticleLog").text(data);
			$("#makeArticleLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
}


/**
 * 生成图书页
 * @returns
 */
function makeBook(){
	var isList = false;
	if($("#makeList").length > 0){
		isList = true;
	}
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeBookForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeBookLog").text("正在生成图书页...");
				taskTimer = setInterval(function(){getMakeProcess('book',start,siteid,isList)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeBookLog").text();
			//$("#makeBookLog").text(data + currentLog);
			$("#makeBookLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
	
}



/**
 * 生成专题页
 * @returns
 */
function makeTopic(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeTopicForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeTopicLog").text("正在生成专题页...");
				taskTimer = setInterval(function(){getMakeProcess('topic',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeTopicLog").text();
			$("#makeTopicLog").text(data + currentLog);
			clearInterval(taskTimer);
		}
	});
	
}


/**
 * 生成资讯列表页
 * @returns
 */
function makeInfoList(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeInfoListForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeInfoListLog").text("正在生成资讯列表页...");
				taskTimer = setInterval(function(){getMakeProcess('infolist',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeInfoListLog").text();
			$("#makeInfoListLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
	
}


/**
 * 生成资讯页
 * @returns
 */
function makeInfo(){
	var siteid = $("#siteid").combobox("getValue");
	var start = new Date();
	var taskTimer;
	$('#makeInfoForm').form('submit', {
		onSubmit : function() {
			if($(this).form('validate')){
				$("#makeInfoLog").text("正在生成资讯详情页...");
				taskTimer = setInterval(function(){getMakeProcess('info',start,siteid)},1000);
			}
			return $(this).form('validate');
		},
		success : function(data) {
			var currentLog = $("#makeInfoLog").text();
			$("#makeInfoLog").text("success " + currentLog);
			clearInterval(taskTimer);
		}
	});
}



/**
 * 获取生成进度
 * @param type
 * @returns
 */
function getMakeProcess(type,start,siteid,isList){
	if(type == "article"){
		$.ajax({
			url:'makehtml/article/process?siteid='+siteid,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					var log = "到达" + data.msg + "," + getTimeCost(start, new Date());
					$("#makeArticleLog").text(log);
				}
			}
		})
	}else if(type == "type"){
		$.ajax({
			url:'makehtml/type/process?siteid='+siteid,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					$("#makeTypeLog").text(data.msg + "," + getTimeCost(start, new Date()));
				}
			}
		})
	}if(type == "info"){
		$.ajax({
			url:'makehtml/info/process?siteid='+siteid,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					var log = "到达" + data.msg + "," + getTimeCost(start, new Date());
					$("#makeInfoLog").text(log);
				}
			}
		})
	}else if(type == "infolist"){
		$.ajax({
			url:'makehtml/infoList/process?siteid='+siteid,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					$("#makeInfoListLog").text(data.msg + "," + getTimeCost(start, new Date()));
				}
			}
		})
	}else if(type == "book"){
		$.ajax({
			url:'makehtml/book/process?siteid='+siteid+"&isList="+isList,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					$("#makeBookLog").text(data.msg + "," + getTimeCost(start, new Date()));
				}
			}
		})
	}else if(type == "topic"){
		$.ajax({
			url:'makehtml/topic/process?siteid='+siteid,
			dataType:'JSON',
			asycn:false,
			success:function(data){
				if(data.success){
					$("#makeTopicLog").text(data.msg + "," + getTimeCost(start, new Date()));
				}
			}
		})
	}
}


/**
 * 一键更新网站
 * @returns
 */
function makeSite(){
	var siteid = $("#siteid").combobox("getValue");
	if(siteid == null || siteid == ""){
		$.messager.alert('Warn', '请选择站点', 'warn');
	}else{
		$("#makeSiteLog1").html("开始生成首页");
		// 生成首页
		$.ajax({
			url : "makehtml/index?makechoose=1&siteid=" + siteid,
			//async : false,
			success : function(result) {
				$("#makeSiteLog1").html("首页生成完成");
			}
		});

		$("#makeSiteLog2").html("开始生成xml地图");
		// 生成xml
		$.ajax({
			url : "makehtml/xml?siteid=" + siteid,
			//async : false,
			success : function(result) {
				$("#makeSiteLog2").html("xml地图生成完成");
			}
		});

		// 生成自定义页面
		$("#makeSiteLog3").html("开始生成自定义页面");
		$.ajax({
			url : "makehtml/custom?siteid=" + siteid,
			//async : false,
			success : function(result) {
				$("#makeSiteLog3").html("自定义页面生成完成");
			}
		});

		// 生成当日资讯详情
		$("#makeSiteLog4").html("开始生成当日资讯详情");
		$.ajax({
			url : "makehtml/article?siteid=" + siteid + "&pubdate="
					+ dateformatter(new Date()),
			//async : false,
			success : function(result) {
				$("#makeSiteLog4").html("当日资讯详情生成完成");
			}
		});

		// 生成资讯列表
		$("#makeSiteLog5").html("开始生成资讯列表");
		$.ajax({
			url : "makehtml/type?siteid=" + siteid,
			//async : false,
			success : function(result) {
				$("#makeSiteLog5").html("资讯列表生成完成");
			}
		});

		// 生成图书列表
		$("#makeSiteLog6").html("开始生成图书列表");
		$.ajax({
			url : "makehtml/book?siteid=" + siteid + "&makeList=1",
			//async : false,
			success : function(result) {
				$("#makeSiteLog6").html("图书列表生成完成");
			}
		});
		 

		// 生成图书详情
		$("#makeSiteLog7").html("开始生成图书详情");
		$.ajax({
			url : "makehtml/book?siteid=" + siteid + "&makeList=0"
					+ "&pubdate=" + dateformatter(new Date()),
			//async : false,
			success : function(result) {
				$("#makeSiteLog7").html("图书详情表生成完成");
			}
		});

		// 生成专题
		/*$("#makeSiteLog8").html("开始生成专题");
		$.ajax({
			url : "makehtml/topic?siteid=" + siteid,
			//async : false,
			success : function(result) {
				$("#makeSiteLog8").html("专题生成完成");
			}
		});*/

	}
}



function getTimeCost(date1, date2){
     var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数      
     //计算出相差天数
     var days=Math.floor(date3/(24*3600*1000))
     //计算出小时数
     var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
     var hours=Math.floor(leave1/(3600*1000))
     //计算相差分钟数
     var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
     var minutes=Math.floor(leave2/(60*1000))
     //计算相差秒数
     var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
     var seconds=Math.round(leave3/1000)
     var cost = "耗时 ";
     if(days > 0){
    	 cost += days + "天";
     }
     if(hours > 0){
    	 cost += hours + "小时";
     }
     if(minutes > 0){
    	 cost += minutes + "分钟";
     }
     if(seconds > 0){
    	 cost += seconds + " 秒";
     }
     return cost;
}

/**
 * 移动端不生成章节
 * @returns
 */
function cancleChapter(){
	$("input#makeChapter[value='0']").prop('checked',true);
	$("input#makeChapter").parents("tr").hide();
}


$(function(){
	
	$("#siteid").combobox({
		onChange : function(newvalue, oldvalue){
			 $('#typeid').combotree({ 
				 url:'commonApi/typeTreeList?siteid=' + newvalue, 
				 valueField:'id', 
				 textField:'name' 
			 }); 
			 
			 $('#topicid').combobox({ 
				 url:'commonApi/topicList?siteid=' + newvalue, 
				 valueField:'id', 
				 textField:'title' 
			 }); 
		}
	})
	
	if($("#pubdate").length > 0){
		$("#pubdate").datebox("setValue", dateformatter(new Date()));
	}
	
})